package com.xjtu.project.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xjtu.project.model.domain.Employee;
import com.xjtu.project.model.dto.EmployeeDTO;
import com.xjtu.project.service.EmployeeService;
import com.xjtu.project.mapper.EmployeeMapper;
import com.xjtu.project.common.utls.SessionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

/**
* @author 86185
* @description 针对表【employee】的数据库操作Service实现
* @createDate 2025-07-04 18:21:25
*/
@Service
public class EmployeeServiceImpl extends ServiceImpl<EmployeeMapper, Employee>
    implements EmployeeService{

    @Autowired
    private EmployeeMapper employeeMapper;

    @Override
    public Page<Employee> selectPagedMy(EmployeeDTO emp) {
       Page<Employee> page = new Page<>(emp.getPage(),emp.getPageSize());
        Page<Employee> page1 = lambdaQuery().like(emp.getEmployeeName() != null, Employee::getEmployeeName, emp.getEmployeeName())
                .eq(emp.getGender() != null && emp.getGender()!="", Employee::getGender, emp.getGender())
                .eq(emp.getCreateTime() != null, Employee::getCreateTime, emp.getCreateTime())
                .eq(emp.getUpdateTime() != null, Employee::getUpdateTime, emp.getUpdateTime()).page(page);
        return page1;
    }

    @Override
    public Employee login(Employee emp) {
        LambdaQueryWrapper<Employee> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(Employee::getEmployeeName,emp.getEmployeeName()).eq(Employee::getPassword,emp.getPassword());
        Employee one = employeeMapper.selectOne(wrapper);
        SessionUtils.saveCurrentEmployeeInfo(one);//把登录用户存进session
        return one;
    }

    @Override
    public Integer addEmployee(Employee emp) {
        emp.setCreateTime(LocalDateTime.now());
        emp.setUpdateTime(LocalDateTime.now());
        int insert = employeeMapper.insert(emp);
        return insert;
    }

    @Override
    public boolean updateByMy(Employee emp) {
        emp.setUpdateTime(LocalDateTime.now());
        boolean b = employeeMapper.updateByIdMy(emp);
        return b;
    }


}




